<?php

/**
 * Description of UcetAdmin
 *
 * @author David Dusek
 */
class ZrealizovanoAdminPresenter extends AdminPresenter {
    public function renderDefault() {
        $estateModel = new EstateModel($this->db);
        $estateModel->where("deleted = ? AND commission_status = ?", array(0,4))->order("id DESC");
        $roles = $this->user->getRoles();
        
        if (!in_array("superadmin", $roles)) {
            $estateModel->where("agent = ?", $this->user->getId());
        }
        $this->template->estates = $estateModel;
        $this->template->agentModel = new AgentModel($this->db);
    }
    
    public function renderPridat() {
        $estateModel = new EstateModel($this->db);
        $estateModel->where("deleted = ?", 1)->order("id DESC");
        $roles = $this->user->getRoles();
        
        if (!in_array("superadmin", $roles)) {
            $estateModel->where("agent = ?", $this->user->getId());
        }
        
        $this->template->estates = $estateModel;
        $this->template->agentModel = new AgentModel($this->db);
    }    
    
    public function handleStahnout($id) {
        $estateModel = new EstateModel($this->db);
        $estate = $estateModel->get($id);
        
        if ($estate === FALSE) {
            $this->flashMessage("Požadová nemovitost neexistuje", "error");
        } else {
            $estateModel->where("id = ?", $id)->update(array("deleted" => 1));
            $this->flashMessage("Nemovitost byla stažena ze zrealizovaných");
        }
        
        $this->invalidateControl();
    }
    
    public function handlePridat($id) {
        $estateModel = new EstateModel($this->db);
        $estate = $estateModel->get($id);
        
        if ($estate === FALSE) {
            $this->flashMessage("Požadová nemovitost neexistuje", "error");
        } else {
            $estateModel->where("id = ?", $id)->update(array("deleted" => 0, "commission_status" => 4));
            $this->flashMessage("Nemovitost byla přidána do zrealizovaných");
        }
        
        $this->invalidateControl();
    }
    
    public function handleSmazat($id) {
        $estateModel = new EstateModel($this->db);
        $estate = $estateModel->get($id);
        
        if ($estate === FALSE) {
            $this->flashMessage("Požadová nemovitost neexistuje", "error");
        } else {
            $estateModel->deleteWithPictures($id);
            $this->flashMessage("Nemovitost odstraněna");
        }
        
        $this->invalidateControl();
    }
}
